#include "send_msg_handle.h"



void spi1_ws2812_rgb_handle(u8 *buf,u16 length)
{
    spi_send_param *spi_param =NULL;    //spi口发送数据参数
    u8 *send_buffer= NULL;
    spi_param = (spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc spi_param in spi1_ws2812_rgb_handle\n");
        return;
    }

    send_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*length);

    if (send_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc send_buffer in spi1_ws2812_rgb_handle\n");
        free(spi_param);
        return;
    }

    
    
    bzero(send_buffer,length);
    memcpy(send_buffer,buf,length); //待发送数据
    spi_param->send_len = length; //待发送数据长度
    spi_param->send_data = send_buffer;
    xQueueSend(spi1_queue_handle,&spi_param,0);
    send_buffer= NULL; //防止野指针
    spi_param= NULL;
     
}

void spi1_ws2812_grb_handle(u8 *buf,u16 length)
{
    spi_send_param *spi_param =NULL;    //spi口发送数据参数
    u8 *send_buffer= NULL;
    spi_param = (spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc spi_param in spi1_ws2812_rgb_handle\n");
        return;
    }

    send_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*length);

    if (send_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc send_buffer in spi1_ws2812_rgb_handle\n");
        free(spi_param);
        return;
    }

    
    
    bzero(send_buffer,length);
    memcpy(send_buffer,buf,length); //待发送数据
    spi_param->send_len = length; //待发送数据长度
    spi_param->send_data = send_buffer;

    xQueueSend(spi1_queue_handle,&spi_param,0);
    send_buffer= NULL; //防止野指针
    spi_param= NULL;
     
}

void spi2_ws2812_rgb_handle(u8 *buf,u16 length)
{
    spi_send_param *spi_param =NULL;    //spi口发送数据参数
    u8 *send_buffer= NULL;
    spi_param = (spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc spi_param in spi1_ws2812_rgb_handle\n");
        return;
    }

    send_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*length);

    if (send_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc send_buffer in spi1_ws2812_rgb_handle\n");
        free(spi_param);
        return;
    }

    
    
    bzero(send_buffer,length);
    memcpy(send_buffer,buf,length); //待发送数据
    spi_param->send_len = length; //待发送数据长度
    spi_param->send_data = send_buffer;

    xQueueSend(spi2_queue_handle,&spi_param,0);
    send_buffer= NULL; //防止野指针
    spi_param= NULL;
     
}

void spi2_ws2812_grb_handle(u8 *buf,u16 length)
{
    spi_send_param *spi_param =NULL;    //spi口发送数据参数
    u8 *send_buffer= NULL;
    spi_param = (spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc spi_param in spi1_ws2812_rgb_handle\n");
        return;
    }

    send_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*length);

    if (send_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc send_buffer in spi1_ws2812_rgb_handle\n");
        free(spi_param);
        return;
    }

    
    
    bzero(send_buffer,length);
    memcpy(send_buffer,buf,length); //待发送数据
    spi_param->send_len = length; //待发送数据长度
    spi_param->send_data = send_buffer;

    xQueueSend(spi2_queue_handle,&spi_param,0);
    send_buffer= NULL; //防止野指针
    spi_param= NULL;
     
}



SEND_SPI_MSG_HANDLE send_spi1_msg_handle[LIGHT_TYPE] = 
{
    {}, //0号位
    {spi1_ws2812_rgb_handle}, //1 RGB
    {spi1_ws2812_grb_handle}, //2 GRB
};

SEND_SPI_MSG_HANDLE send_spi2_msg_handle[LIGHT_TYPE] = 
{
    {}, //0号位
    {spi2_ws2812_rgb_handle}, //1 RGB
    {spi2_ws2812_grb_handle}, //2 GRB
};


void send_spi1_msg(u8 *buf,u16 length) //一号发送引脚
{
    send_spi1_msg_handle[SPI1_LIGHT_TYPE].handle(buf,length);
}

void send_spi2_msg(u8 *buf,u16 length) //二号发送引脚
{
    send_spi2_msg_handle[SPI2_LIGHT_TYPE].handle(buf,length);
}